package io.github.talelin.latticy.controller.v1.ag;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.ag.room.CreateOrUpdateRoomDTO;
import io.github.talelin.latticy.model.ag.RoomDO;
import io.github.talelin.latticy.model.ag.RoomDetailDO;
import io.github.talelin.latticy.service.ag.RoomService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/v1/ag/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @GetMapping("/{id}")
    public RoomDO getRoom(@PathVariable(value = "id")@Positive(message = "id必须为正整数")int id){
        RoomDO room = roomService.getById(id);
        if (room==null){
            throw new NotFoundException("未找到房间",10023);
        }
        return room;
    }


    @GetMapping("/page")
    public PageResponseVO<RoomDetailDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{count}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page}") Integer page
    ) {
        Page<RoomDetailDO> pager = new Page<>(page, count);
        IPage<RoomDetailDO> paging =roomService.getRoomByPage(pager);
        return PageUtil.build(paging);
    }

    @GetMapping("/list")
    public List<RoomDetailDO> getRooms(){
        List<RoomDetailDO> rooms = roomService.getRooms();
        if (rooms==null){
            throw new NotFoundException("未找到房间");
        }
        return rooms;
    }

    @GetMapping("/building_to_room/{id}")
    public List<RoomDetailDO> getRoomsByBuildingId(@PathVariable(value = "id")@Positive(message = "id必须为正整数")int id){
        List<RoomDetailDO> rooms = roomService.getRoomsByBuildingId(id);
        if (rooms==null){
            throw new NotFoundException("未找到房间");
        }
        return rooms;
    }

    @GetMapping("/search")
    public PageResponseVO<RoomDetailDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{count}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page}") Integer page,
            @RequestParam(name = "keyword") String keyword
    ) {
        Page<RoomDetailDO> pager = new Page<>(page, count);
        IPage<RoomDetailDO> paging = roomService.searchRoomByKeyword(pager, keyword);
        return PageUtil.build(paging);
    }

    @PostMapping("/create")
    public CreatedVO createRoom(@RequestBody @Validated CreateOrUpdateRoomDTO validator){
        roomService.createRoom(validator);
        return new CreatedVO("创建房间成功");
    }
    @DeleteMapping("{id}")
    @PermissionMeta(value = "删除房间", module = "房间", mount = true)
    @GroupRequired
    public DeletedVO deletedRoom(@PathVariable("id") @Positive(message = "{id}") int id){
        RoomDO room = roomService.getById(id);
        if (room == null){
            throw new NotFoundException("building not found",10022);
        }
        roomService.deleteById(room.getId());
        return new DeletedVO(16);
    }

    @PutMapping("{id}")
    public UpdatedVO updatedRoom(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                                     @RequestBody @Validated CreateOrUpdateRoomDTO validator){
        RoomDO room = roomService.getById(id);
        if (room == null){
            throw new NotFoundException("building not found",10022);
        }
        roomService.updateRoom(room,validator);
        return new UpdatedVO("更新房间成功");
    }
}
